//
//  HMStatusOriginalView.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/2/1.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
import SnapKit

class HMStatusOriginalView: UIView {
    var pictureConstraints: Constraint?
    
    var statusViewModel: HMStatusViewModel?{
        didSet{
            iconView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLable.text = "刚刚"
            sourceLable.text = "来自哪里"
            vipImageView.image = statusViewModel?.vipImage
            verifiedImageView.image = statusViewModel?.verifiedImg
            contentLabel.text = statusViewModel?.status?.text
            
            pictureConstraints?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
                pictureView.pic_urls = pic_urls
                
                //显示控件
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.pictureConstraints = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
                })
                
                
            }else{
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.pictureConstraints = make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
                })
            }
    }
    }
    override init(frame: CGRect) {
      super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){ 
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HMStatusCellMargin)
            make.top.equalTo(self).offset(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(HMStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLable.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLable.snp_trailing ).offset(HMStatusCellMargin)
            make.centerY.equalTo(timeLable)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(HMStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.pictureConstraints =  make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载控件
    //头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    //时间
    private lazy var timeLable: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 10)
    //来自
    private lazy var sourceLable: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 10)
    //会员
    private lazy var vipImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
    //文字
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, maxWidth: SCREENW - HMStatusCellMargin * 2)
    private lazy var pictureView: HMStatusPictureView = {
        let view = HMStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
